<?php if ( ! DEFINED('BASEPATH') ) die('No direct script access allowed.');

/**
 * @author 		Mark Skilbeck
 * @copyright 	Copyright (c) 2009, Mahcuz.com, Inc.
 */

class project extends Controller {
	 
	public function project ( )
	{
		parent::Controller();
		
		// Load necessary libraries, models, etc.
			$this->load->model('project_model');
			$this->load->library('theme');
			$this->load->library('form_validation');
		
		// Load profiler - debugging only.
			$this->output->enable_profiler(TRUE);
			
		// Initialise Theme and load theme.
			Theme::init();
			Theme::load_theme_type('admin');
	 	 	
	}
	
	public function index ( )
	{
		# No methods called. Show admin/project menu.
		$vars['area']   = 'admin';
		$vars['module'] = 'project/main';
		$vars['theme_type'] = Theme::$theme_type;
		$vars['theme_alias'] = Theme::$theme_alias;
		$this->load->view('loader', $vars);
	}
	
	public function create ( )
	{
		$vars['area'] 	= 'admin';
		$vars['module']	= 'project/create';
		$vars['theme_type'] = Theme::$theme_type;
		$vars['theme_alias'] = Theme::$theme_alias;
		
		# Form was submitted
		if ($this->input->post('create') !== FALSE) 
		{
			# Set of validation rules.
			$rules = array(
				array('field' => 'project_name', 'label' => 'Project Name', 'rules' => 'trim|required|'.
					  'min_length[4]|max_length[100]|callback_project_not_exists'),
		  		array('field' => 'project_author', 'label' => 'Project Author', 'rules' => 'trim|required|'.
    				  'min_length[4]|max_length[16]'),
				array('field' => 'project_description', 'label' => 'Project Description', 'rules' => 'trim|'.
					  'min_length[4]')
			);
			$this->form_validation->set_rules($rules);
			if ($this->form_validation->run() === FALSE)
			{
				# Form did not validate successfully.
				$this->load->view('loader', $vars);
			}
			else
			{
				# Form validated.
				# Create alias for project name, and add new project to database.
				$this->load->helper('alias_helper');
				$alias = create_alias($this->input->post('project_name'));
				$data = array (
					'project_name' 	 => $this->input->post('project_name'),
					'project_author' => $this->input->post('project_author'),
					'project_desc' 	 => $this->input->post('project_description'),
					'alias' 		 => $alias
				);
				$this->project_model->create_project($data);
				
				$vars['project'] = $this->project_model->get_project_by_alias($alias);
				$vars['page'] = 'success';
				$this->load->view('loader', $vars);
			}
		}
		else 
		{
			# Load the create project form.
			# It will load the 'content.php' file by default, which holds the form.
			$this->load->view('loader', $vars);
		}
	}
	
	function edit ( )
	{
		$vars['area'] 	= 'admin';
		$vars['module'] = 'project/edit';
		$vars['theme_type'] = Theme::$theme_type;
		$vars['theme_alias'] = Theme::$theme_alias;
		
		# Do we have a project alias given?
		if ( $this->uri->segment(4) !== FALSE )
		{
			# If a proj alias is supplied, our project model will automatically
			# grab the data for it (if the proj alias doesn't resolve to any known
			# projects, the data is set to NULL).
			if ( $this->project_model->name === NULL )
			{
				show_error('That project does not exist.');
			}

			# Do we have a submitted form to work with?
			if ( $this->input->post('edit') !== FALSE )
			{
				# Validate, etc.
				$rules = 
					array (
						array (
							'field' => 'project_name',
							'label' => 'Project Name',
							'rules' => 'trim|required|min_length[4]|max_length[100]|callback_project_not_exists_if_updated'
						),
						array (
							'field' => 'project_author',
							'label' => 'Project Author',
							'rules' => 'trim|required|min_length[4]|max_length[60]'
						),
						array (
							'field' => 'project_description',
							'label' => 'Project Description',
							'rules' => 'trim|required|min_length[1]'
						)
					);
				$this->form_validation->set_rules($rules);

				if ( $this->form_validation->run() !== TRUE )
				{
					$vars['page'] = 'edit_form';
					$vars['project'] = $this->project_model;
					$this->load->view('loader', $vars);
				}
				else
				{
					$this->load->helper('alias_helper');
					$alias = create_alias($this->input->post('project_name'));
					$data = 
						array (
							'project_name' 		=> $this->input->post('project_name'),
							'project_author' 	=> $this->input->post('project_author'),
							'project_desc' 		=> $this->input->post('project_description'),
							'alias' 			=> $alias
						);

					$this->project_model->update_project($this->uri->segment(4), $data);

					$vars['page'] = 'success';
					$vars['project'] = $this->project_model->get_project_by_alias($alias);
					$this->load->view('loader', $vars);
				}
			}
			else
			{
				$vars['page'] = 'edit_form';
				$vars['project'] = $this->project_model->get_project_by_alias($this->uri->segment(4));
				$this->load->view('loader', $vars);
			}
		}
		else 
		{
			$vars['projects'] = $this->project_model->get_all_projects();
			$this->load->view('loader', $vars);
		}
	}
	
	function delete ( )
	{
		$vars['area'] 	= 'admin';
		$vars['module']	= 'project/delete';
		$vars['theme_type'] = Theme::$theme_type;
		$vars['theme_alias'] = Theme::$theme_alias;
		
		# We have a project alias.
		if ($this->uri->segment(4) !== FALSE) 
		{
			# Make sure the project alias points to a valid project.
			if ( $this->project_model->name === NULL )
			{
				show_error('That project does not exist.');
			}
			
			$vars['project'] = $this->project_model->get_project_by_alias($this->uri->segment(4));
			
			# We have confirmation.
			if ($this->uri->segment(5) !== FALSE && $this->uri->segment(5) == 'do') 
			{
				# Delete the project and it's child releases, changelogs, etc.
				$this->project_model->delete_changelogs($this->project_model->name);
				$this->project_model->delete_releases($this->project_model->name);
				$this->project_model->delete_project($this->project_model->name);
				
				$vars['page'] = 'success';
				$this->load->view('loader', $vars);
			}
			else 
			{
				$vars['page'] = 'confirm';
				$this->load->view('loader', $vars);
			}
		}
		else 
		{
			$vars['projects'] = $this->project_model->get_all_projects();
			$this->load->view('loader', $vars);
		}
	}
	
	// ----------------------------------------------------------
	// CALLBACKS USED BY VALIDATION LIBRARY
	// For lack of a better way, they're in here. Clutter!
	// ----------------------------------------------------------
	
	/**
	 * Project::project_not_exists()
	 * 
	 * Checks whether a project doesn't exist.
	 * 
	 * @param  string	$proj_name
	 * @access public
	 * @return bool
	 */
	function project_not_exists( $proj_name )
	{
		# Project_model::get_project() returns NULL if project doesn't exist.
		if ( $this->project_model->get_project($proj_name) === NULL )
		{
			return TRUE;
		}
		else
		{
			# We want the alias, so we'll grab the proj info.
			$project = $this->project_model->get_project($proj_name);
			$proj_url = site_url('main/view/' . $project->alias);
			
			$this->form_validation->set_message('project_not_exists', 
			 									"That project <a href='{$proj_url}'>already exists</a>.");
			unset($project);
			unset($proj_url);
			return FALSE;
		}
	}
	
	/**
	 * Project::project_not_exists_if_updated()
	 * 
	 * This is similar to Project::project_not_exists(), but it will only
	 * call Project_model::get_project() if the param given is different
	 * to the current project name (in the Project_model::_project_name)
	 * property.
	 * 
	 * @param 	string $proj_name
	 * @return 	bool
	 */
	function project_not_exists_if_updated( $proj_name )
	{
		if ( $proj_name !== $this->project_model->name )
		{
			if ( $this->project_model->get_project($proj_name) === NULL )
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}
		return TRUE;
	}
}

?>